﻿package RecursiveSearcher;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LC39
 * @Description TODO
 * @Author ZJX
 * @Date 2024/10/23 9:22
 * @Version 1.0
 */
public class LC39 {
    List<List<Integer>> ret;
    List<Integer> path;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        ret = new ArrayList<>();
        path = new ArrayList<>();

        dfs(candidates,target,0,0);
        return ret;
    }

    private void dfs(int[] candidates, int target, int sum, int startIndex) {
        for (int i = startIndex; i <= candidates.length; i++) {
            if (sum > target){
                return;
            }
            if (target == sum){
                ret.add(new ArrayList<>(path));
                return;
            }

            path.add(candidates[i]);
            dfs(candidates,target,sum + candidates[i],i );
            path.remove(path.size() - 1);
        }
    }
}
